<template>
  <div class="page-wrapper page-h5-transaction">
    <van-list
      ref="vanListRef"
      v-model:loading="loading"
      :finished="finished"
      :finished-text="list.length === 0 ? '暂无消息' : '没有更多了'"
      @load="onLoad"
    >
      <div class="task-wrapper">
        <van-form
          v-for="(item, idx) of list"
          :key="idx"
          class="task-item"
          @click="handleDetail(item)"
        >
          <van-cell-group inset>
            <van-field name="switch">
              <template #input>
                <van-badge style="width: 100%" dot>
                  {{ item.order_id }} : {{ item.message }}
                </van-badge>
              </template>
            </van-field>
          </van-cell-group>
        </van-form>
      </div>
    </van-list>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { useRouter } from 'vue-router';
  import { apiGetNoticePage, apiSetNoticeState } from '@/api/dispatch';
  import { useUserStore } from '@/store/index';

  const router = useRouter();
  const { account, driver_id } = useUserStore();
  const list = ref<any[]>([]);

  let pageNum = 1;
  const pageSize = 10;
  const loading = ref(false);
  const finished = ref(false);

  const vanListRef = ref();

  const onLoad = () => {
    loading.value = true;
    const params: any = {
      page_num: pageNum,
      page_size: pageSize,
      driver_id,
      read_status: 0,
    };

    apiGetNoticePage(params)
      .then((res) => {
        list.value.push(...res.data.rows);
        // 加载状态结束
        loading.value = false;
        pageNum += 1;
        // 数据全部加载完成
        if (res.data.rows.length < pageSize) {
          finished.value = true;
        }
      })
      .catch(() => {
        loading.value = false;
        finished.value = true;
      });
  };

  const handleDetail = (item: any) => {
    apiSetNoticeState(item.id);
    router.push({
      name: 'taskDetail',
      query: {
        id: item.order_id,
      },
    });
  };
</script>

<style lang="less" scoped>
  .task-item {
    position: relative;
    margin-top: 20px;
    width: 100%;
    .title {
      color: #999;
    }
    .btns {
      padding: 20px;
      text-align: right;
    }

    .status {
      position: absolute;
      right: 60px;
      top: 28px;
      z-index: 10;
      color: blue;
      font-size: 12px;
    }
  }
</style>
